//
//  FavoriteCell.m
//  WorldOfFacts_V2
//
//  Created by Demjanko Denis on 19.11.12.
//  Copyright (c) 2012 it-dimension. All rights reserved.
//

#import "FavoriteCell.h"
#import "PhotoBox.h"
#import "UIImageView+WebCache.h"

@implementation FavoriteCell

#pragma mark - init

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {  
        separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comments_separator.png"]];
        separator.frame = CGRectMake(0, 98, 320, 2);
        [self.contentView addSubview:separator];
        
        photoBox = [[PhotoBox alloc] initWithFrame:CGRectMake(7, 10, 80, 80)];
        [self.contentView addSubview:photoBox];
        
        badge_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"badge-favorite"]];
        badge_favorite.frame = CGRectMake(295, 0, 25, 25);
        [self.contentView addSubview:badge_favorite];
        
        self.textLabel.shadowColor = [UIColor whiteColor];
        self.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
        self.textLabel.numberOfLines = 3;
        self.textLabel.highlightedTextColor = [UIColor clearColor];

        self.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue-Italic" size:12];
                
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    return self;
}

#pragma mark - Layout

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGSize textLabelSize = [self.textLabel.text sizeWithFont:self.textLabel.font  constrainedToSize:CGSizeMake(190, 80) lineBreakMode:self.textLabel.lineBreakMode];
    
    self.textLabel.frame = CGRectMake(95, 5, 195, textLabelSize.height);
    
    self.detailTextLabel.frame = CGRectMake(95, textLabelSize.height+5, 190, 20);
}

#pragma mark - Data

-(void)setFact:(NSMutableDictionary*)fact{    
    self.textLabel.text = [fact valueForKey:@"mini_description"];
    self.detailTextLabel.text = [fact valueForKey:@"date"];
    
    NSString *icon_url = [[fact valueForKey:@"icon"] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    NSString* escapedUrlString = [icon_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [photoBox startIndicator:YES];
    
    [photoBox setImageWithURL:[NSURL URLWithString:escapedUrlString] placeholderImage:nil isScaled:YES size:CGSizeMake(160, 160)];
}

#pragma mark - Memory

-(void)dealloc{
    badge_favorite = nil;
    photoBox = nil;
    separator = nil;
}

@end
